package org.askil.eshift.ui.user;

import org.askil.eshift.ui.user.table.UserTableBean;
import org.askil.eshift.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalSplitPanel;

public class UserAdminUI extends CustomComponent {

	private AbsoluteLayout mainLayout;
	private VerticalSplitPanel verticalSplitPanel_1;
	private Panel panel_1;
	private FormLayout formLayout;
	private TextField firstNameTf;
	private Table userTable;
	private TextField lastNameTf;
	private TextField emailTf;

	private static final Logger LOG = LoggerFactory.getLogger(UserAdminUI.class);

	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public UserAdminUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

		userTable.setColumnReorderingAllowed(true);

		userTable.setColumnHeader(UserTableBean.FIRST_NAME, "First Name");
		userTable.setColumnHeader(UserTableBean.LAST_NAME, "Last Name");
		userTable.setColumnHeader(UserTableBean.EMAIL, "Email");
		userTable.setColumnHeader(UserTableBean.CTRL_USER, "Added By");
		userTable.setColumnHeader(UserTableBean.CTRL_DATE, "Added On");
		userTable.setColumnHeader(UserTableBean.USER_ROLES, "User Roles");



	}

	@Override
	public void attach() {
		super.attach();

		User user = (User) getApplication().getUser();
		if ( user == null ){
			LOG.error("No user set.");
			return;
		}
		
	}

	public void setModel(UserAdminModel model){
		userTable.setContainerDataSource(model.getUserTableModel());

		userTable.setVisibleColumns(new Object[] {UserTableBean.FIRST_NAME
				,UserTableBean.LAST_NAME
				,UserTableBean.EMAIL
				,UserTableBean.CTRL_USER
				,UserTableBean.CTRL_DATE
				,UserTableBean.USER_ROLES
		});
	}

	private AbsoluteLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);

		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");

		// verticalSplitPanel_1
		verticalSplitPanel_1 = buildVerticalSplitPanel_1();
		mainLayout.addComponent(verticalSplitPanel_1, "top:0.0px;left:0.0px;");

		return mainLayout;
	}

	private VerticalSplitPanel buildVerticalSplitPanel_1() {
		// common part: create layout
		verticalSplitPanel_1 = new VerticalSplitPanel();
		verticalSplitPanel_1.setImmediate(false);
		verticalSplitPanel_1.setWidth("100.0%");
		verticalSplitPanel_1.setHeight("100.0%");
		verticalSplitPanel_1.setMargin(false);

		// userTable
		userTable = new Table();
		userTable.setImmediate(false);
		userTable.setWidth("100.0%");
		userTable.setHeight("100.0%");
		verticalSplitPanel_1.addComponent(userTable);

		// panel_1
		panel_1 = buildPanel_1();
		verticalSplitPanel_1.addComponent(panel_1);

		return verticalSplitPanel_1;
	}

	private Panel buildPanel_1() {
		// common part: create layout
		panel_1 = new Panel();
		panel_1.setImmediate(false);
		panel_1.setWidth("100.0%");
		panel_1.setHeight("100.0%");

		// verticalLayout_1
		formLayout = buildFormLayout();
		panel_1.setContent(formLayout);

		return panel_1;
	}

	private FormLayout buildFormLayout() {
		// common part: create layout
		formLayout = new FormLayout();
		formLayout.setImmediate(false);
		formLayout.setWidth("100.0%");
		formLayout.setHeight("100.0%");
		formLayout.setMargin(false);

		firstNameTf = new TextField("First Name");
		firstNameTf.setImmediate(false);
		firstNameTf.setWidth("-1px");
		firstNameTf.setHeight("-1px");
		firstNameTf.setRequired(true);
		formLayout.addComponent(firstNameTf);

		lastNameTf = new TextField("Last Name");
		lastNameTf.setImmediate(false);
		lastNameTf.setWidth("-1px");
		lastNameTf.setHeight("-1px");
		lastNameTf.setRequired(true);
		formLayout.addComponent(lastNameTf);

		emailTf = new TextField("Email");
		emailTf.setImmediate(false);
		emailTf.setWidth("-1px");
		emailTf.setHeight("-1px");
		emailTf.setRequired(true);
		formLayout.addComponent(emailTf);


		return formLayout;
	}

}
